package com.qire.manhua.model.vo;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.CustomEvent;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseVO;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;

public class UserDetailVO extends BaseVO {

    public UserBean        user;
    public InviteConfig    invite_config;          //{InviteConfig}
    public BalanceVO       balance;                //余额数据
    public MessageBean     message;                //: {MessageBean},
    public TaskBean        task;                   //: {TaskBean}

    public void updateUserEntity() {
        updateUserEntityInfo();
        updateUserEntityBalance();
        updateUserEntityTaskAndMsg();

        NoticeEventBus.NOTICE.sendEvent(CustomEvent.USER_UPDATE_REFRESH);
    }

    private void updateUserEntityInfo() {
        if(ObjectUtil.isNull(user)) {
            return;
        }
        if (user.isForbidden()) {
            UserEntity.self.logout();
            ToastHelper.showSnackBar(user.forbidden_text, Prompt.WARNING);
            return;
        }
        UserEntity.self.setId(user.userid);
        UserEntity.self.setName(user.user_name);
        UserEntity.self.setPortrait(user.user_img);
        UserEntity.self.setSex(user.user_sex);
        UserEntity.self.setBirthday(user.birthday);
        UserEntity.self.setCityInfo(user.city, user.cityName, user.provinceName);
        UserEntity.self.setMobile(user.mobile);
        UserEntity.self.setWeChatName(user.wx_name);
        UserEntity.self.setQqName(user.qq_name);
        UserEntity.self.setVipType(user.vipInfo.vipType);
        UserEntity.self.setVipExpirationTime(user.vipInfo.expiration_time);
        UserEntity.self.setTest(user.is_test);
        UserEntity.self.setForbidden(user.is_forbidden);
        UserEntity.self.setVisitor(user.is_visitor);
        UserEntity.self.setAutoPay(user.is_autopay);
        UserEntity.self.setEva(user.is_eva);
        UserEntity.self.save();
    }

    private void updateUserEntityBalance() {
        if(ObjectUtil.nonNull(balance)) {
            UserEntity.self.setBeanBalance(StringUtil.valueToFloat(balance.bean, 0));
            UserEntity.self.setCoinBalance(StringUtil.valueToFloat(balance.coin, 0));
        }
    }

    private void updateUserEntityTaskAndMsg() {
        if(ObjectUtil.nonNull(message)) {
            UserEntity.self.setMessageCount(message.msg);
        }
        if(ObjectUtil.nonNull(task)) {
            UserEntity.self.setTaskCount(task.not_num);
            UserEntity.self.setTaskRewardCount(task.pending_num);
        }
    }

    public class UserBean {
        private int         userid;                 //: 10002,
        private String      user_name;              //: "阿钟姐",
        private String      user_img;               //: "http://thirdqq.qlogo.cn/qqapp/101406064/4BC272AD9165160D0E94B850F5D18743/100",
        private int         user_sex;               //: 1,
        private String      birthday;               //: "2018-09-18",
        private int         city;                   //: 113004,
        private String      provinceName;           //: "湖南",
        private String      cityName;               //: "郴州",
        private int         is_test;                //: 0,
        private VipInfoVO   vipInfo;                //: 2,
        private int         is_forbidden;           //: 是否禁用 1 禁用 0 未禁用
        private String      forbidden_text;         //: 禁用信息
        private int         is_visitor;             //: 0,
        private int         is_autopay;             //: 0,
        private int         is_eva;                 //: 1,
        private String      mobile;                 //: "17343752357",
        private String      wx_name;                //: "",
        private String      qq_name;                //: "阿*姐"
        private int         is_mobile;              //: 1,是否绑定手机 1是 0否
        private int         is_weixin;              //: 0,
        private int         is_weixin_subscribe;    //: 0,
        private int         is_qq;                  //: 1

        public boolean isForbidden() {
            return is_forbidden == 1;
        }

        public class VipInfoVO{
            public int    vipType;              //: 0,
            public String expiration_time;      //: "2020-02-07 14:00:00"
        }
    }

    public static class InviteConfig{
        public String text;     //:'邀请好友',
        public String tips;     //:'可获得次元币'
    }

    public class BalanceVO {
        public String bean;               //: "0.0",
        public String coin;               //: "5000.0",
    }

    public class MessageBean {
        public int msg;                //信息消息数: 2,
    }

    public static class TaskBean {
        public int not_num;             //未完成任务数
        public int pending_num;         //完成待领取任务数
    }

}
